package com.faker.core.generate.data.code.common.primary;

import lombok.extern.slf4j.Slf4j;

/**
 * 自增ID生成生成器
 */
@Slf4j
public class IncreaseIdGenerator {

    // 自增ID起始位置
    private static Long increaseIdStart = 0l;
    private static String tableNameLast;

    /**
     * 设置自增ID起始位置
     *
     * @param start
     */
    public static void setIncreaseIdStart(Long start) {
        increaseIdStart = start;
    }

    /**
     * 获取自增ID
     *
     * @return
     */
    public static Long getIncreaseId(String tableName) {
        //如果生成的表格名称发生变化则归零
        if (!tableName.equals(tableNameLast)) {
            //序列归零
            setIncreaseIdStart(0l);
            tableNameLast = tableName;
        }

        Long increaseId = increaseIdStart;
        if (increaseIdStart >= 9223372036854775807l) {
            log.error("Long类型超出最大范围,自动归零");
            increaseIdStart = 0l;
        } else {
            increaseIdStart++;
        }
        return increaseId;
    }


}
